<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    this.areaDict = new Map();
    const fs = require("fs");
    const data = fs.readFileSync("./data/area.txt", "utf8");

    for (const line of data.split("\n")) {
      const [prefix, code, name] = line.trim().split("\t");
      //这里的每个键都要存一个二个元素的数组 所以占用内存较高
      //this.areaDict.set(prefix, [code, name]);  //直接使用Map存储，会导致冗余数据过多
      //用一个对象存这样会节省空间
      this.areaDict[prefix] = {
        code,
        name
      };
      //如果对数据进行查询可以使用遍历对象属性，Object.keys
      function searchAreaDict(prefix) {
        for (const key of Object.keys(areaDict)) {
          if (prefix.startsWith(key)) {
            return areaDict[key];
          }
        }
        return null;
      }
    }
  </script>
</body>

</html>